/** FASSIT (Forms of Selective Attention in Intelligent Transportation Systems)
 * Computational Creativity and Digital Media
 * Cognitive and Media Systems Group
 * Centre for Informatics and Systems of the University of Coimbra (CISUC)
 *
 * Copyright (c) 2010-2013 University of Coimbra, Portugal
 * All rights reserved.
 */

package masterAgent.Knowledge;

import java.util.ArrayList;
import java.util.HashMap;

import masterAgent.DBmanager.DBConnection;
import masterAgent.DBmanager.DBManager;
import poi.Info.POI;
import poi.Info.POIwithSurprise;
import poi.Surprise.SurpriseDaysOff;
import poi.Surprise.SurprisePrice;
import poi.Surprise.SurpriseSchedule;
import poi.SurpriseCalculator.SurpriseCalculatorForPOI;
import agent.Context.Context;
import agent.Context.Goal;

/**
 * This class is used as an Interface to the Agent communicate with the
 * MasterAgent
 * 
 * @author Hernani Costa
 * @version 0.1 date: 2/12/2012
 */
public class MasterAgent {
	private DBManager manager;
	private HashMap<Integer, POIwithSurprise> noisyPOIs;

	public MasterAgent() {
		manager = new DBManager();
		noisyPOIs = new HashMap<Integer, POIwithSurprise>();
	}

	/**
	 * ------------------------------------------------------------------------
	 * Returns all the POIs within a radius for a specific Goal
	 * ------------------------------------------------------------------------
	 * 
	 * 
	 * @param context
	 *            - agent context
	 * @return an arrayList with all the POIs within the radius for a specific
	 *         goal
	 */
	public HashMap<Integer, POI> getPOIsNear(Context context) {
		return manager.getPOIsNear(context.getLocation(), context.getRadius(),
				context.getGoal());
	}

	/**
	 * 
	 * @param pois
	 * @return
	 */
	public HashMap<Integer, POIwithSurprise> getKnowledge(
			HashMap<Integer, POI> pois) {
		return getPOIsWithSurprise(pois);
	}

	/**
	 * This method creates the new object POIwithSurprise. It converts
	 * automatically a list of POIs to a list of POIWithSurprise
	 * 
	 * @param pois
	 *            - the list of POIs to be converted
	 * @return POIwithSurprise - the resulted list of POIs with Surprise
	 */
	private HashMap<Integer, POIwithSurprise> getPOIsWithSurprise(
			HashMap<Integer, POI> pois) {
		SurpriseCalculatorForPOI maSurprise = new SurpriseCalculatorForPOI(pois);

		HashMap<Integer, SurpriseSchedule> poiSurpriseSchedule = maSurprise
				.getPoiSurpriseSchedule();
		HashMap<Integer, SurpriseDaysOff> poiSurpriseDayOff = maSurprise
				.getPoiSurpriseDayOff();
		HashMap<Integer, SurprisePrice> poiSurprisePrice = maSurprise
				.getPoiSurprisePrice();

		HashMap<Integer, POIwithSurprise> poisWithSurprise = new HashMap<Integer, POIwithSurprise>();
		// ArrayList<POIwithSurprise> poisWithSurprise = new
		// ArrayList<POIwithSurprise>();
		int poiID = 0;
		POIwithSurprise poiWithSurprise = null;

		for (POI poi : pois.values()) {
			poiID = poi.getPoiID();
			// System.err.println(poi.to_String());
			poiWithSurprise = new POIwithSurprise(poi,
					poiSurprisePrice.get(poiID), poiSurpriseDayOff.get(poiID),
					poiSurpriseSchedule.get(poiID));
			// System.err.println("POIid: "+poiID);
			// System.err.println(poiWithSurprise.to_String());
			poisWithSurprise.put(poiID, poiWithSurprise);
			// poisWithSurprise.add(poiWithSurprise);
		}

		return poisWithSurprise;
	}

	public HashMap<Integer, POI> getNoisyPOIs(
			HashMap<Integer, POI> poisWithSurprise, Context context) {
		NoisyChannel noisyChannel = new NoisyChannel(poisWithSurprise, context);
		return noisyChannel.getNoisyPOIs();
	}

	/**
	 * ------------------------------------------------------------------------
	 * Only for debug purpose
	 * ------------------------------------------------------------------------
	 */
	public static void main(String args[]) {
		DBConnection connector = new DBConnection();

		POI poi = connector.getPOI(1507669);
		DBManager manager = new DBManager();

		HashMap<Integer, POI> pois = manager.getPOIsNear(poi.getLocation(),
				200, new Goal().getCoffee());
		MasterAgent k = new MasterAgent();
		HashMap<Integer, POIwithSurprise> poisWithSurprise = k
				.getKnowledge(pois);

		for (POIwithSurprise p : poisWithSurprise.values()) {
			System.out.println(p.to_String());

		}
	}
}
